package org.ellisande.grid.tags;

import javax.servlet.jsp.JspWriter;

import org.apache.commons.lang3.StringUtils;
import org.ellisande.grid.tags.support.TagBuilder;
import org.ellisande.tld.annotations.Attribute;
import org.ellisande.tld.annotations.Description;
import org.ellisande.tld.annotations.Info;
import org.ellisande.tld.annotations.Tag;

@Tag
@Info("Tag for adding a radio button to the page.")
public class RadioTag extends InputTag {

	private static final long serialVersionUID = 4307302872297337697L;

	@Attribute
	@Description("Value that will be associated with this radio button choice")
	private String value = "";

	@Attribute
	@Description("Label that will be associated with this radio button")
	private String label = "";

	private static final String LABEL_STYLE = "radio";

	@Override
	public void startTag() throws Exception {
		TagBuilder inputBuilder = createInputTagBuilder();
		TagBuilder labelBuilder = new TagBuilder("label");
		if (!StringUtils.isEmpty(value)) {
			inputBuilder.addAttribute("value", value);
		}
		inputBuilder.addAttribute("type", "radio");

		labelBuilder.addStyle(LABEL_STYLE);
		JspWriter out = pageContext.getOut();

		if (!StringUtils.isEmpty(label)) {
			out.write(labelBuilder.compileStartTag());
		}

		out.write(inputBuilder.compileStartTag());
	}

	@Override
	public void endTag() throws Exception {
		TagBuilder labelBuilder = new TagBuilder("label");

		JspWriter out = pageContext.getOut();
		out.write(label);
		out.write(labelBuilder.compileEndTag());
	}

	@Override
	public void release() {
		super.release();
		value = null;
		label = null;
	}

	public void setLabel(String label) {
		this.label = label;
	}

	public void setValue(String value) {
		this.value = value;
	}

}
